package com.jyw.activity;


import android.annotation.SuppressLint;
import android.app.FragmentTransaction;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.jyw.R;
import com.jyw.fragment.HomeFragment;
import com.jyw.fragment.MeFragment;
import com.jyw.fragment.OrderFragment;
import com.jyw.network.OkHttpManager;
import com.jyw.network.Server;
import com.jyw.utils.DimenUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @author simonw
 * @version 1.0
 * @created 2016/12/29
 */

public class MainActivity extends BaseActivity implements View.OnClickListener {
    private ImageView ivHome, ivMe, ivOrder, ivBg;
    private View vHome;
    private int flag = 0;

    @RequiresApi(api = Build.VERSION_CODES.GINGERBREAD)
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ivHome = (ImageView) findViewById(R.id.iv_home);
        ivMe = (ImageView) findViewById(R.id.iv_me);
        ivOrder = (ImageView) findViewById(R.id.iv_order);
        ivBg = (ImageView) findViewById(R.id.iv_bg);
        vHome = findViewById(R.id.v_home);
        vHome.setOnClickListener(this);
        findViewById(R.id.v_me).setOnClickListener(this);
        findViewById(R.id.v_order).setOnClickListener(this);

        vHome.performClick();

        todayFirstToast();
    }

    private void todayFirstToast() {
        @SuppressLint("SimpleDateFormat")
        Request request = new Request.Builder().
                url(Server.TODAY_FIRST + "?phone=" + getPrefs().getString("phone", "") +
                        "&time=" + new SimpleDateFormat("yyyyMd").format(new Date())).
                build();
        OkHttpManager.getInstance().getOkHttpClient().newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                OkHttpManager.backgroundThreadShortToast(MainActivity.this, "错误：" + e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String res = response.body().string();
                try {
                    JSONObject json = new JSONObject(res);
                    if (json.getString("error").equals("error")) {

                    } else {
                        OkHttpManager.backgroundThreadShortToast(MainActivity.this,
                                "积分：+" + json.getString("integral")
                                        + "；余额：+" + json.getString("balance"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        FragmentTransaction tr = getFragmentManager().beginTransaction();
        switch (v.getId()) {
            case R.id.v_home:
                chageHome();
                tr.replace(R.id.container, HomeFragment.newInstance());
                break;
            case R.id.v_me:
                chageMe();
                tr.replace(R.id.container, MeFragment.newInstance());
                break;
            case R.id.v_order:
                chageOrder();
                tr.replace(R.id.container, OrderFragment.newInstance());
                break;
        }
        tr.commit();
    }

    private void chageHome() {
        LinearLayout.LayoutParams paramsHome = (LinearLayout.LayoutParams) ivHome.getLayoutParams();
        LinearLayout.LayoutParams paramsMe = (LinearLayout.LayoutParams) ivMe.getLayoutParams();
        LinearLayout.LayoutParams paramsOrder = (LinearLayout.LayoutParams) ivOrder.getLayoutParams();
        paramsHome.height = DimenUtils.dp2px(MainActivity.this, 38);
        paramsMe.height = ViewGroup.LayoutParams.MATCH_PARENT;
        paramsOrder.height = ViewGroup.LayoutParams.MATCH_PARENT;
        ivHome.setImageResource(R.drawable.home_big);
        ivMe.setImageResource(R.drawable.me_small);
        ivOrder.setImageResource(R.drawable.order_small);
        ivBg.setImageResource(R.drawable.bg_home);
        flag = 0;
    }

    private void chageMe() {
        LinearLayout.LayoutParams paramsHome = (LinearLayout.LayoutParams) ivHome.getLayoutParams();
        LinearLayout.LayoutParams paramsMe = (LinearLayout.LayoutParams) ivMe.getLayoutParams();
        LinearLayout.LayoutParams paramsOrder = (LinearLayout.LayoutParams) ivOrder.getLayoutParams();
        paramsHome.height = ViewGroup.LayoutParams.MATCH_PARENT;
        paramsMe.height = DimenUtils.dp2px(MainActivity.this, 38);
        paramsOrder.height = ViewGroup.LayoutParams.MATCH_PARENT;
        ivHome.setImageResource(R.drawable.home_small);
        ivMe.setImageResource(R.drawable.me_big);
        ivOrder.setImageResource(R.drawable.order_small);
        ivBg.setImageResource(R.drawable.bg_me1);
        flag = 1;
    }

    private void chageOrder() {
        LinearLayout.LayoutParams paramsHome = (LinearLayout.LayoutParams) ivHome.getLayoutParams();
        LinearLayout.LayoutParams paramsMe = (LinearLayout.LayoutParams) ivMe.getLayoutParams();
        LinearLayout.LayoutParams paramsOrder = (LinearLayout.LayoutParams) ivOrder.getLayoutParams();
        paramsHome.height = ViewGroup.LayoutParams.MATCH_PARENT;
        paramsMe.height = ViewGroup.LayoutParams.MATCH_PARENT;
        paramsOrder.height = DimenUtils.dp2px(MainActivity.this, 38);
        ivHome.setImageResource(R.drawable.home_small);
        ivMe.setImageResource(R.drawable.me_small);
        ivOrder.setImageResource(R.drawable.order_big);
        ivBg.setImageResource(R.drawable.bg_order);
        flag = 2;
    }

    @Override
    public void onBackPressed() {
        if (flag != 0) {
            vHome.performClick();
        } else {
            finish();
        }
    }
}
